package durose.system.controller;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import durose.durose.DuRoseGenerator;
import durose.system.dict.CommonDict;
import durose.system.dict.ResponseDict;
import durose.system.mapper.DefineMapper;
import durose.system.pojo.DuRose;
import durose.system.service.Sys_menuService;

/**
 * 杜玫瑰代码生成控制器
 * @author Administrator
 *
 */
@Controller
public class RoseHeartController {
	String prefix="system/durose/";
	
	@Autowired
	DefineMapper defineMapper;
	@Autowired 
	 Sys_menuService sys_menuService;
	
	
	@RequestMapping("durose_generator")
	public String durose(Model model){
		
		List<String> tableNames = defineMapper.getTables();
		tableNames = removeTables(tableNames);
		model.addAttribute("tables", tableNames);
		model.addAttribute("sys_menus", sys_menuService.sys_menuParentList(0));
		return prefix+"durose_generator";
	}

	
	@RequestMapping("durose_add")
	@ResponseBody
	public Object duroseAdd(@RequestBody List<DuRose> duroses,HttpServletRequest request){
		DuRoseGenerator durose = new DuRoseGenerator(request.getServletContext());
		durose.DuRoseInit(duroses); 
		return new ResponseDict(CommonDict.BUS_STATUS_OK);
	}
	
	/**
	 * 根据选择的表名查询出所有的列名
	 */
	@RequestMapping("durose_getColumns")
	@ResponseBody
	public Object duroseGetColumns(String tableName){
		List<String> strs = defineMapper.getColumns(tableName);
		return strs;
	}
	/**
	 * 查询出所有的表名
	 */
	@RequestMapping("durose_getTableNames")
	@ResponseBody
	public Object duroseGetTableNames(){
		List<String> strs = defineMapper.getTables();
		return strs;
	}
	
	/**
	 * 排除需要生成的表
	 */
	private List<String> removeTables(List<String> strs){
		List<String> tablesNew = strs;
		List<String> tables = Arrays.asList(CommonDict.REMOVE_TABLES);
		for(String table:tables){
			if(tablesNew.contains(table)){
				tablesNew.remove(table);
			}
		}
		return tablesNew;
	}
	
	
}
